//
//  ListViewController.m
//  QQ好友列表
//
//  Created by TianGe-ios on 14-8-21.
//  Copyright (c) 2014年 TianGe-ios. All rights reserved.
//

// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com

#import "ListViewController.h"
#import "FriendGroup.h"
#import "Friend.h"
#import "HeadView.h"
//#import "ViewController.h"

@interface ListViewController () <HeadViewDelegate>
{
    NSArray *_friendsData;
    
}
@end

@implementation ListViewController

- (id)initWithData:(NSDictionary*)dataDic
{
    self = [super init];
    if (self) {
        //[self loadData:dataDic type:0];
    }
    return self;
}

- (id)initWithDataFrame:(CGRect)frame{
    self = [super init];
    if (self) {
        
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"好友列表";
    self.tableView.sectionHeaderHeight = 40;
    self.view.backgroundColor = kWhiteColor;
    
    self.tableView.tableFooterView = [UIView new];
    
    
    
    //[self loadData:nil];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
}

#pragma mark 加载数据
- (void)loadData:(NSDictionary*)dataDic type:(int)typeInt leverl:(int)leverlInt
{
    //NSURL *url = [[NSBundle mainBundle] URLForResource:@"friends.plist" withExtension:nil];
    //NSArray *tempArray = [NSArray arrayWithContentsOfURL:url];
    /*
    NSDictionary *ddd00 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"11111",@"icon",
                           @"22222",@"intro",
                           @"aaaaaaaa",@"name",
                           [NSNumber numberWithInt:20],@"vip",
                           nil];
    
    NSDictionary *ddd01 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"11111",@"icon",
                           @"22222",@"intro",
                           @"aaaaaaaa",@"name",
                           [NSNumber numberWithInt:20],@"vip",
                           nil];
    
    
    NSDictionary *ddd02 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"11111",@"icon",
                           @"22222",@"intro",
                           @"aaaaaaaa",@"name",
                           [NSNumber numberWithInt:20],@"vip",
                           nil];
    
    NSArray *aaa = [[NSArray alloc] initWithObjects:ddd00,ddd01,ddd02, nil];
    
    NSDictionary *DDD = [NSDictionary dictionaryWithObjectsAndKeys:
                         aaa, @"friends",
                         @"bbbbbb", @"name",
                         [NSNumber numberWithInt:20], @"online",
                         nil];
    
    
    
    NSDictionary *ddd10 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"11111",@"icon",
                           @"22222",@"intro",
                           @"aaaaaaaa",@"name",
                           [NSNumber numberWithInt:20],@"vip",
                           nil];
    
    NSDictionary *ddd11 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"11111",@"icon",
                           @"22222",@"intro",
                           @"aaaaaaaa",@"name",
                           [NSNumber numberWithInt:20],@"vip",
                           nil];
    
    
    NSDictionary *ddd12 = [NSDictionary dictionaryWithObjectsAndKeys:
                           @"11111",@"icon",
                           @"22222",@"intro",
                           @"aaaaaaaa",@"name",
                           [NSNumber numberWithInt:20],@"vip",
                           nil];
    
    NSArray *aaa111 = [[NSArray alloc] initWithObjects:ddd10,ddd11,ddd12, nil];
    
    NSDictionary *DDD111 = [NSDictionary dictionaryWithObjectsAndKeys:
                            aaa111, @"friends",
                            @"ccccccc", @"name",
                            [NSNumber numberWithInt:20], @"online",
                            nil];
    */
    NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:3];//[[NSArray alloc] initWithObjects:DDD, DDD111, nil];
    
    
    
    NSArray *dataList = [dataDic objectForKey:@"DataList"];
    
    for (NSDictionary *tempD in dataList) {
        NSArray *aa = [tempD objectForKey:@"OrgList"];
        if (leverlInt == 0) {
            aa = [tempD objectForKey:@"OrgList"];
        }else{
            aa = [tempD objectForKey:@"ProjectList"];
        }
        int count = 0;
        NSMutableArray *muA = [[NSMutableArray alloc] initWithCapacity:3];
        for (NSDictionary *hhh in aa) {
            NSDictionary *dic;  //= [[NSDictionary alloc] initWithObjectsAndKeys:
                                //[hhh objectForKey:@"OrgName"] ,@"name",
                                // [NSNumber numberWithInt:[[hhh objectForKey:@"AnalysisCount"] intValue]], @"vip",
                                // nil];
            if (leverlInt == 0) {
                dic = [[NSDictionary alloc] initWithObjectsAndKeys:
                       [hhh objectForKey:@"OrgName"] ,@"name",
                       [NSNumber numberWithInt:[[hhh objectForKey:@"AnalysisCount"] intValue]], @"vip",
                       nil];
            }else{
                dic = [[NSDictionary alloc] initWithObjectsAndKeys:
                       [hhh objectForKey:@"ProjectName"] ,@"name",
                       [NSNumber numberWithInt:[[hhh objectForKey:@"AnalysisCount"] intValue]], @"vip",
                       nil];
            }
            [muA addObject:dic];
            count += [[hhh objectForKey:@"AnalysisCount"] intValue];
        }
        
        NSString *nameStr = @"";
        if (typeInt == 0) {
            NSArray *tempT = [[NSUserDefaults standardUserDefaults] objectForKey:Dic_RiskType];
            if (tempT != nil && tempT.count > 0) {
                for (NSDictionary *fff in tempT) {
                    if ([[fff valueForKey:@"Value"] intValue] == [[tempD objectForKey:@"RiskType"] intValue]) {
                        nameStr = [fff valueForKey:@"Name"];
                    }
                }
            }
        }else if(typeInt == 1){
            NSArray *tempT = [[NSUserDefaults standardUserDefaults] objectForKey:Check_Type_Items];
            if (tempT != nil && tempT.count > 0) {
                for (NSDictionary *fff in tempT) {
                    if ([[fff valueForKey:@"Value"] intValue] == [[tempD objectForKey:@"InspectType"] intValue]) {
                        nameStr = [fff valueForKey:@"Name"];
                    }
                }
            }
        }else if(typeInt == 2){
            NSArray *tempT = [[NSUserDefaults standardUserDefaults] objectForKey:select_RiskLevel];
            if (tempT != nil && tempT.count > 0) {
                for (NSDictionary *fff in tempT) {
                    if ([[fff valueForKey:@"Value"] intValue] == [[tempD objectForKey:@"RiskLevel"] intValue]) {
                        nameStr = [fff valueForKey:@"Name"];
                    }
                }
            }
        }else{
            nameStr = [tempD valueForKey:@"ItemName"];
        }
        
        NSDictionary *tempDic = [[NSDictionary alloc] initWithObjectsAndKeys:
                                 muA, @"friends",
                                 nameStr, @"name",
                                 [NSNumber numberWithInt:count], @"online",
                                 nil];
        [tempArray addObject:tempDic];
    }
    
    NSMutableArray *fgArray = [NSMutableArray array];
    for (NSDictionary *dict in tempArray) {
        FriendGroup *friendGroup = [FriendGroup friendGroupWithDict:dict];
        [fgArray addObject:friendGroup];
    }
    
    _friendsData = fgArray;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _friendsData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    FriendGroup *friendGroup = _friendsData[section];
    NSInteger count = friendGroup.isOpened ? friendGroup.friends.count : 0;
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    FriendGroup *friendGroup = _friendsData[indexPath.section];
    Friend *friend = friendGroup.friends[indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:friend.icon];
    cell.textLabel.textColor = kGreenColor;//friend.isVip ? [UIColor redColor] : [UIColor blackColor];
    cell.textLabel.text = friend.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%d", friend.vip];
    
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeadView *headView = [HeadView headViewWithTableView:tableView];
    headView.contentView.backgroundColor = kWhiteColor;
    headView.delegate = self;
    headView.friendGroup = _friendsData[section];
    
    return headView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    ViewController *viewController = [[ViewController alloc] init];
//    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)clickHeadView
{
    [self.tableView reloadData];
    if ([self.delegate respondsToSelector:@selector(passReload)]) {
        [self.delegate passReload];
    }
}

@end
